package com.twu.biblioteca.command;

import com.twu.biblioteca.BibliotecaSystem;
import com.twu.biblioteca.User;
import com.twu.biblioteca.util.Messages;
import sun.plugin2.message.Message;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/21/13
 * Time: 6:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class LoginCommand extends Command {

    private Scanner scanner;
    private BibliotecaSystem system;

    public LoginCommand(BibliotecaSystem system) {
        this.system = system;
        this.scanner = system.getScanner();
    }

    @Override
    public void runCommand() {
        login();
    }

    private void login() {
        System.out.print("username: ");
        String username = scanner.nextLine();

        System.out.print("password: ");
        String password = scanner.nextLine();

        User user = User.authenticate(username, password);
        if (user == null) {
            System.out.println(Messages.Login_Failed);
        } else {
            System.out.println(Messages.Login_Successfully);
            system.setLoggedUser(user);
        }
    }
}
